from django.db import models

from persons.models import Person
from teacher.models import TeacherModel

YESANDNO = (
    (0, "否"),
    (1, "是"),
)


# 学生
class StudentModel(models.Model):
    openid = models.CharField("openid", max_length=50, null=True, blank=True)
    phone = models.CharField("手机", max_length=50, unique=True)
    dept = models.CharField("部门", max_length=50)
    name = models.CharField("姓名", max_length=50)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    @property
    def is_authenticated(self):
        return True

    def __str__(self):
        return f"学生<{self.name}>"

    class Meta:
        unique_together = ("openid", "phone")
        verbose_name = verbose_name_plural = "学生管理"


# 学生访客
class StudentPerson(models.Model):
    student = models.ForeignKey(StudentModel, on_delete=models.CASCADE, verbose_name="学生")
    person = models.ForeignKey(Person, on_delete=models.CASCADE, verbose_name="访客")
    order = models.IntegerField("顺序", default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.student.__str__()}-{self.person.__str__()}"

    class Meta:
        unique_together = ("student", "person")
        verbose_name = verbose_name_plural = "学生访客"


# 学生邀约
class StudentInvite(models.Model):
    STATUS_CHOISE = (
        (0, "教师审核中"),
        (1, "保卫处审核中"),
        (5, "审核完成"),
        (6, "审核失败"),
        (7, "访问中"),
        (8, "完成访问"),
        (9, "访问过期"),
        (10, "取消"),
    )
    status = models.IntegerField("状态", choices=STATUS_CHOISE, default=0)
    student = models.ForeignKey(StudentModel, on_delete=models.CASCADE, verbose_name="邀请者")
    person = models.ForeignKey(Person, on_delete=models.CASCADE, verbose_name="访客")
    what = models.CharField("工作内容", max_length=500)
    when = models.DateTimeField("工作日期")
    created = models.DateTimeField("创建时间", auto_now_add=True, blank=True, null=True)
    updated = models.DateTimeField("更新时间", auto_now=True, blank=True, null=True)

    def __str__(self):
        return f"{self.student.__str__()}邀约"

    class Meta:
        verbose_name = verbose_name_plural = "学生邀约管理"



# 学生邀约访客
class StudentInvitePerson(models.Model):
    invite = models.ForeignKey(StudentInvite, on_delete=models.CASCADE, verbose_name="学生邀约")
    person = models.ForeignKey(Person, on_delete=models.CASCADE, verbose_name="访客")
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.invite.__str__()}-{self.person.__str__()}"

    class Meta:
        unique_together = ("invite", "person")
        verbose_name = verbose_name_plural = "学生邀约的访客管理"


# 学生邀约审核
class StudentInviteVerify(models.Model):
    invite = models.ForeignKey(StudentInvite, on_delete=models.CASCADE, verbose_name="学生邀约")
    STATGE_CHOISE = (
        (0, "教师审核中"),
        (1, "保卫处审核中"),
        (5, "审核完成"),
        (6, "审核失败"),
        (7, "访问中"),
        (8, "完成访问"),
        (9, "访问过期"),
        (10, "取消"),
    )
    stage = models.IntegerField("状态", choices=STATGE_CHOISE, default=0)
    STATUS_CHOISE = (
        (0, "待审核"),
        (1, "审核通过"),
        (2, "审核失败"),
    )
    status = models.IntegerField("结果", choices=STATUS_CHOISE, default=0)
    teacher = models.ForeignKey(TeacherModel, on_delete=models.CASCADE, verbose_name="审核者")
    describe = models.CharField("描述", max_length=512)
    created = models.DateTimeField("创建时间", auto_now_add=True, blank=True, null=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.invite.__str__()}审核"

    class Meta:
        unique_together = ('invite', 'stage')
        verbose_name = verbose_name_plural = "学生邀约的审核管理"
